package com.chidopi.market.bookstore.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chidopi.cdf.service.IBaseService;
import com.chidopi.cdf.struts1actions.CdfDispatchAction;
import com.chidopi.cdf.util.ModelContextFactory;
import com.chidopi.market.aa.service.ICityPermissionService;
import com.chidopi.market.bookstore.domain.BookCityExt;
import com.chidopi.market.bookstore.service.IBookItemsService;

public class CityConfAction extends CdfDispatchAction {
	private static Logger logger = LoggerFactory.getLogger(CityConfAction.class);
	private ICityPermissionService cityPService;
	private IBookItemsService baseService;
	
	public CityConfAction() {
		try {
			this.cityPService = 
					ModelContextFactory.getT("cityPermissionService", ICityPermissionService.class);			
		} catch (Exception e) {
			logger.warn("init", e);
		}
		
		try {
			this.baseService = (IBookItemsService)
				ModelContextFactory.get("bookItemsService", IBaseService.class);
		} catch (Exception e) {
			this.baseService = null;
			logger.warn("initial IBookItemsService", e);
		}
	}
	
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uri = request.getRequestURI();
		String funcUri = uri.substring(uri.lastIndexOf('/') + 1);
		logger.debug("init) {}", funcUri);
		request.getSession().setAttribute("funcurl", funcUri);
		this.entryURL = "/" + funcUri;
		Integer userId = (Integer) request.getSession().getAttribute("user_id");
		int city_id = this.cityPService.getCityPermission(userId, funcUri);
		if (city_id >0) {
			BookCityExt t = this.baseService.getBookCityExtById(city_id);
			if (null == t) {
				return mapping.findForward("failure");
			}
			DynaBean dynaForm = (DynaBean) form;
			dynaForm.set("vo", t);
			return mapping.findForward("success");
		} else {
			return mapping.findForward("failure");
		}
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BookCityExt t = new BookCityExt();
		DynaBean dynaForm = (DynaBean) form;
		try {
			PropertyUtils.copyProperties(t, dynaForm.get("vo"));
			Integer userId = (Integer) request.getSession().getAttribute("user_id");
			int city_id = this.cityPService.getCityPermission(userId, (String) request.getSession().getAttribute("funcurl"));
			if (city_id > 0) { 
				t.setCity_id(city_id);
				this.baseService.updateBookCityExt(t);
				logger.debug("entryURL = {}", this.entryURL);
				request.setAttribute("entryURL",  entryURL.replaceFirst("\\.do$", ""));
				return mapping.findForward("success");
			}
			logger.warn("{} has no permission for {}", userId, city_id);
			return mapping.findForward("failure");
		} catch (Exception e) {
			logger.warn("update fail", e);
			return mapping.findForward("failure");
		}
	}
}
